
<?php
//模块插件类
//对外接口函数类，其他插件可通过“$this->plugin('jy_juhesms')->方法名” 来调用本类的方法
defined('BY_JYA') or exit('error');
class func_jy_juhesms extends plugin{
	public function sendsms($mobile,$content,$tpl_id,$pluginname=''){
		$setting=$this->get_setting();
		$appkey=$setting['appkey'];
		$sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
		$smsConf = array(
			'key'   => $appkey, //您申请的APPKEY
			'mobile'    => $mobile, //接受短信的用户手机号码
			'tpl_id'    => $tpl_id, //您申请的短信模板ID，根据实际情况修改
			'vars' => ijson_encode($content), //您设置的模板变量，根据实际情况修改
		);
		$content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
		$result = json_decode($content,true);
		$reason = $result['reason'];
		if($result['error_code']){
			$status=0;
		}else{
			$status=1;
		}
		$logdata=array(
			'uniacid'=>$_SESSION['uniacid'],
			'plugin'=>$pluginname,
			'tpl_id'=>$tpl_id,
			'remark'=>$reason,
			'mobile'=>$mobile,
			'status'=>$status,
			'createtime'=>TIMESTAMP
		);	
		$this->insert('log',$logdata);
		return $status;
	}
	
    private function juhecurl($url,$params=false,$ispost=0){
		$httpInfo = array();
		$ch = curl_init();
		curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
		curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
		curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
		curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
		if( $ispost )
		{
			curl_setopt( $ch , CURLOPT_POST , true );
			curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
			curl_setopt( $ch , CURLOPT_URL , $url );
		}
		else
		{
			if($params){
				curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
			}else{
				curl_setopt( $ch , CURLOPT_URL , $url);
			}
		}
		$response = curl_exec( $ch );
		if ($response === FALSE) {
			//echo "cURL Error: " . curl_error($ch);
			return false;
		}
		$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
		$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
		curl_close( $ch );
		return $response;
	}
}
?>
